import re,time,os,json,pandas as pd


def num_cm(x,site,flag=False):
    if not x : 
        #处理空值None和Na是直接返回空
        if pd.isna(x):return
        return
    def num_5(num,flag=flag,site=site):
        if flag:
            return str(round(num,2))
        if isinstance(num,float) and site.lower() not in ['uk','inch','us']:    
            if 0<num<=10:
                return str(int(num+0.5))
            else:
                return str(int(((num+2.5)//5)*5))
        if isinstance(num,float) and site.lower() in ['uk','inch','us']:
            if num:
                return str(int(num+0.5))
            else:
                return str(int(((num+2.5)//5)*5))
    def num_cm2(num,type_=1,flag=flag):

        if site in ['us','uk','inch']:unit,un=' inch',2.54
        else:unit,un=' cm',1
        num_times=[2.54,1,0.1,30.48,100]
        pos_=''
        if re.findall(r'(?<![\w])(height|tall|h|H)(?![\w])',num):pos_=' H'      
        if re.findall(r'(?<![\w])(l|L|long|length)(?![\w])',num):pos_=' L'
        if re.findall(r'(?<![\w])(w|W|width)(?![\w])',num):pos_=' W'
        if re.findall(r'(?<![a-zA-Z])((D|d)(iameter)?|Ø|ø)(?![a-zA-Z])',num):pos_=' Diameter'
        num=re.sub(r'(?<=[0-9])((\.|\x20){2}|\,)(?=[0-9])','.',num)
        str_sum='''(?<![0-9\.\(])([0-9]*((?<=[0-9])[\.\,\/](?=[0-9])))*[0-9]+\x20?(inches|(-)?(I|i)nch|(C|c)(M|m)|\""|\"|\″|(-)?(F|f)eet|ml|mm|cc|oz|in|\''|\'|\“|\”|\’’|\’|(M|m)illimetres?|centimet(ers|res)?|ft)?(?!\))'''
        num_list=[j.group() for j in re.finditer(str_sum,num) if j]
        if len(num_list)>1:
            num_sum=0
            for i in num_list:
                num1,pos1=num_cm2(i,type_,flag=True).split(unit)
                if pos1:pos_=pos1 
                else:pos_=pos_  
                num_sum+=float(num1)
            return num_5(num_sum)+unit+pos_
        elif num_list:
            num=num_list[0]
        if 'in' in num or r'"' in num or r"''" in num or '”' in num:type_=num_times[0]/un
        elif 'cm' in num or r'centimet' in num:type_=num_times[1]/un
        elif 'mm' in num or 'millimet' in num:type_=num_times[2]/un
        elif 'feet' in num or 'ft' in num or "'" in num:type_=num_times[3]/un
        elif 'oz' in num or 'ounce' in num:return#unit,flag=' oz',True
        elif 'ml' in num or 'gram' in num:return#unit,flag=' ml',True
        elif 'm ' in num:type_=num_times[4]/un
        # else:return '(ø 8.8 '
        if '/' in num:num=str(eval(re.search('([0-9]*((?<=[0-9])[\.\,\/](?=[0-9])))*[0-9]+',num).group()))
        if pos_==' Diameter':
            tmp_diameter=num_5(float(re.search(r'\x20?([0-9]+\.)?(\x20?[0-9]\x20?)+',num).group().replace(' ',''))*type_,flag=flag)+unit
            return f'{tmp_diameter} x {tmp_diameter}{pos_}'
        return num_5(float(re.search(r'\x20?([0-9]+\.)?(\x20?[0-9]\x20?)+',num).group().replace(' ',''))*type_,flag=flag)+unit+pos_

    def num_cm1(x,site=site):
        
        x=x.lower()
        num_sep=[j.group() for j in re.finditer(r'(×|х|x|X|\*|by)|((?<![0-9]),|,(?![0-9]))',x) if j]
        if len(num_sep) >=1:
            num_list = x.split(num_sep[0])
        else:
            num_list=[x]
        num_times=[2.54,1,0.1,30.48,100]
        if site.lower() in ['uk','us','inch']:
            num_times=list(map(lambda x: x/2.54,num_times))
        if 'in' in x or r'"' in x or r"''" in x or '”' in x:
            size_cm = ' x '.join([str(num_cm2(i,num_times[0])) for i in num_list])
        elif 'cm' in x or r'centimet' in x:
            size_cm = ' x '.join([str(num_cm2(i,num_times[1])) for i in num_list])
        elif 'mm' in x or 'millimet' in x:
            size_cm = ' x '.join([str(num_cm2(i,num_times[2])) for i in num_list])
        elif 'feet' in x or 'ft' in x or "'" in x:
            size_cm = ' x '.join([str(num_cm2(i,num_times[3])) for i in num_list])
        elif 'm ' in x:
            size_cm = ' x '.join([str(num_cm2(i,num_times[4])) for i in num_list])
        # elif '倍率' in x:
            # size_cm = ' x '.join([str(num_cm2(i,倍率)) for i in num_list])
            
        else:
            #默认不带任何单位的处理方式
            size_cm = ' x '.join([str(num_cm2(i)) for i in num_list if num_cm2(i)])
        if size_cm:
            return size_cm
        else:
            return
    # print(x)
    def sum_size(x):
        str_split='''(?<![0-9])(?<![0-9],)[0-9]*((?<=[0-9])[\.\,](?=[0-9]))?[0-9]+(\x20|-)?(inches|(-)?(I|i)nch|(C|c)(M|m)|\"|\″|(-)?(F|f)eet|ml|mm|cc|oz|in|\''|\'|\“|\”|\’’|M|m|centimetre|ft)'''
        size_list=[i.group() for i in re.finditer(str_split,x)]
        if len(size_list)>1:
            # pass
            num_sum=0
            for i in list(map(num_cm1,x)):
                num_sum += float(i.split()[0])
            num_sum = int(num_sum+0.5)
            return f'{num_sum} {i.split()[1]}'
        else:
            size_1 =num_cm1(x,site)
        return size_1
    if isinstance(x,list):
        size_cm = [i for i in map(num_cm1,x) if i]
        
    if isinstance(x,str):
        size_cm = num_cm1(x)
    return size_cm

if __name__ == '__main__':
    print(num_cm('''3'6"''','us'))
